/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Himanshu  Sharma
 */
import javax.swing.* ;
import java.awt.event.* ;
import java.util.*;
//import javax.swing.GroupLayout.Group ;


public class Centipede {

    Centipede(){
        this.money = 2;
        this.init_count = 5;
        InitComponents() ;
         
        Run();
    }

    Centipede( int money, int count){
        this.money = money;
        this.count = count;
        InitComponents();
        
        Run();
    }
    Game G;
    JFrame cframe ;
    JPanel p1,p2 ;
    JLabel l1 ;
    JLabel l2, l3, l4 ;
    JButton b1, b2 ;
    int money=5, money_Won = 0;
    int count = 0, disable=0, init_count ;
    Random generator;


    public static void main(String [] args) {
        Centipede c = new Centipede() ;
    }

    public void InitComponents() {
        cframe = new JFrame("Centipede") ;
        cframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        cframe.setVisible(true);

        G = new Game();

        p1 = new javax.swing.JPanel();
        l1 = new javax.swing.JLabel();
        l2 = new javax.swing.JLabel();
        l3 = new javax.swing.JLabel();
        l4 = new javax.swing.JLabel();
        b1 = new javax.swing.JButton();
        b2 = new javax.swing.JButton();

        cframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



        javax.swing.GroupLayout p1Layout = new javax.swing.GroupLayout(p1);
        p1.setLayout(p1Layout);
        p1Layout.setHorizontalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p1Layout.createSequentialGroup()
                .addContainerGap(88, Short.MAX_VALUE)
                .addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(61, 61, 61))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(l2, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, p1Layout.createSequentialGroup()
                .addContainerGap(96, Short.MAX_VALUE)
                .addComponent(l3, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94))
        );
        p1Layout.setVerticalGroup(
            p1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(p1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(l1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(l2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(l3)
                .addContainerGap(16, Short.MAX_VALUE))
        );


        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(cframe.getContentPane());
        cframe.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(181, 181, 181)
                .addComponent(b2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 232, Short.MAX_VALUE)
                .addComponent(b1)
                .addGap(177, 177, 177))
            .addGroup(layout.createSequentialGroup()
                .addGap(225, 225, 225)
                .addComponent(p1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(227, 227, 227))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(339, Short.MAX_VALUE)
                .addComponent(l4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(335, 335, 335))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addComponent(p1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(l4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(94, 94, 94)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(b1)
                    .addComponent(b2))
                .addGap(43, 43, 43))
        );

        l1.setText("You are player 1");
        l2.setText("           What would you like to do?");
        l3.setText("You have");
        l4.setText("$" + Integer.toString(money));
        b1.setText("Pass");
        b2.setText("Stop");

        cframe.pack();
    }

    public void Run(){
        cframe.addMouseListener(new CheckMouse());
        b1.addActionListener(new PassListener());
        b2.addActionListener(new StopListener());
    }

    class PassListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            if (disable ==0) {
                generator = new Random();
                int choice;
                choice = generator.nextInt(3) ;
                if(choice==2){
                     l2.setText("               Player 2 accepted $" + money);
                     l3.setText("You Lost!!!");
                     disable =1;
                     
                     money_Won = 0 ;
                     b1.setVisible(false);
                     b2.setText("Next");
                     
                 }
                 else{
                    money = money + 2 ;
                    l4.setText("$" +Integer.toString(money));
                    l2.setText("Player 2 passed what'd you like to do?");
                    count = count+1 ;
                    if ( count == init_count ) {
                        l2.setText("");
                        l1.setText("");
                        l1.setText("Time Out!!!") ;
                        l3.setText("");
                        money_Won = 0 ;
                        b1.setVisible(false);
                        b2.setText("Next");
                    }

            }
    }
        }
    }

    class StopListener implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            
            if(b2.getText().equals("Stop")) {
                l1.setText("You won $"+money);
                l2.setText("");
                l3.setText("");
                disable=1;
                
                money_Won = money;
                b1.setVisible(false);
                b2.setText("Next");
            }
            else if(b2.getText().equals("Next")){
                try{
                G.writeLog("Centipede: $ " + Integer.toString(money_Won) + " Number_of_passes " + count  );
                }catch(Exception ex){
                    G.writeFile("err.txt", ex.getMessage());
                    //ex.printStackTrace();
                }
                cframe.setVisible(false); 
                G.free = true; 

    }
    }
}
        class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}

        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
            if(b2.getText().equals("Stop")) {
                l1.setText("You won $"+money);
                l2.setText("");
                l3.setText("");
                disable=1;

                money_Won = money;
                b1.setVisible(false);
                b2.setText("Next");
            }
            else if(b2.getText().equals("Next")){
                try{
                G.writeLog("Centipede: $ " + Integer.toString(money_Won) + " Number_of_passes " + count  );
                }catch(Exception ex){
                    G.writeFile("err.txt", ex.getMessage());
                    ex.printStackTrace();
                }
                cframe.setVisible(false);
                G.free = true;

            }
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
              System.out.println("Middle button pressed.");
            }
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                if (disable ==0) {
                    generator = new Random();
                    int choice;
                    choice = generator.nextInt(3) ;
                    if(choice==2){
                         l2.setText("               Player 2 accepted $" + money);
                         l3.setText("You Lost!!!");
                         disable =1;

                         money_Won = 0 ;
                         b1.setVisible(false);
                         b2.setText("Next");

                     }
                     else{
                        money = money + 2 ;
                        l4.setText("$" +Integer.toString(money));
                        l2.setText("Player 2 passed what'd you like to do?");
                        count = count+1 ;
                        if ( count == init_count ) {
                            l2.setText("");
                            l1.setText("");
                            l1.setText("Time Out!!!") ;
                            l3.setText("");
                            money_Won = 0 ;
                            b1.setVisible(false);
                            b2.setText("Next");
                        }
                     }
                }
            }
        }
    }
    
}





